//
//  GFOtherTagsViewController.m
//  GoodFood
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 荣伟峰. All rights reserved.
//

#import "GFOtherTagsViewController.h"

@interface GFOtherTagsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tagsTableView;
@property (nonatomic, copy) NSString *otherTags;
@property (nonatomic, copy) NSString *bus;
@end

@implementation GFOtherTagsViewController
#pragma mark - 初始化
- (instancetype)initWithTags:(NSString *)otherTags withBus:(NSString *)busInfo {
    self = [super init];
    if (self) {
        self.otherTags = otherTags;
        self.bus = busInfo;

    }
    return self;
}
#pragma mark - 导航
- (void)viewForNavi {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    view.backgroundColor = [UIColor redColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth / 2, ScreenHeight / 3, ScreenWidth / 3, ScreenHeight / 4)];
    label.text = @"其他信息";
    label.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.7];
    label.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = label;
    self.view.backgroundColor = [UIColor lightGrayColor];
}
#pragma mark - 创建tableView
- (void)creatTableView {
    UIView *view = (UIView *)[self.navigationController.view viewWithTag:10001];
    self.tagsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, view.frame.size.height, ScreenWidth, ScreenHeight) style:UITableViewStyleGrouped];
    self.tagsTableView.delegate = self;
    self.tagsTableView.dataSource = self;
    self.tagsTableView.bounces = NO;
    [self.tagsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tags"];
    [self.tagsTableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"head"];
    [self.view addSubview:self.tagsTableView];
}
#pragma mark - row number
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
#pragma mark - section number
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
//    NSInteger i = [self judgeRowAndSection];
    return 2;

}
#pragma mark - cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tags"];
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    cell.textLabel.numberOfLines = 0;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (0 == indexPath.section) {
        if (!(nil == self.otherTags) && (![self.otherTags isEqualToString:@""])) {
            NSRange range = {self.otherTags.length - 1, 1};
            self.otherTags = [self.otherTags stringByReplacingCharactersInRange:range withString:@""];
        } else {
            self.otherTags = @"暂无信息";
        }

        cell.textLabel.text = self.otherTags;
    } else {
        if (!(nil == self.bus) && (![self.bus isEqualToString:@""])) {
            cell.textLabel.text = self.bus;
        } else {
            cell.textLabel.text = @"暂无公交信息";
        }
        
    }
    return cell;
}
#pragma mark - 自定义高度
- (CGFloat)heightForCellWithStr:(NSString *)string {
    CGRect rect = [string boundingRectWithSize:CGSizeMake(ScreenWidth, 0) options:1 attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    return rect.size.height;
}
#pragma mark - 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.section) {
        CGFloat height = [self heightForCellWithStr:self.otherTags];
        return height + 20;
    } else {
        CGFloat height = [self heightForCellWithStr:self.bus];
        return height + 20;
    }
}
#pragma mark - 判断行数和区数方法
- (NSInteger)judgeRowAndSection {
    NSInteger i = 0;
    if (!(nil == self.otherTags) && (![self.otherTags isEqualToString:@""])) {
        i ++;
    }
    if (!(nil == self.bus) && (![self.bus isEqualToString:@""])) {
        i ++;
    }
    return i;
}
#pragma mark - tableView 头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewHeaderFooterView *view = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"head"];
    if (0 == section) {
        view.textLabel.text = @"餐厅特色";
    } else {
        view.textLabel.text = @"公交信息";
    }
    return view;
}
#pragma mark - section head height
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return ScreenWidth / 10;
}

#pragma mark - 视图加载
- (void)viewDidLoad {
    [super viewDidLoad];
    [self viewForNavi];
    [self creatTableView];
    
}
@end
